<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="团队名称" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入团队名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="签约数量" prop="signedNumber">-->
<!--        <el-input-->

<!--          v-model="queryParams.signedNumber"-->
<!--          placeholder="请输入签约数量"-->
<!--          clearable-->

<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="团队简介" prop="teamInfo">-->
<!--        <el-input-->
<!--          v-model="queryParams.teamInfo"-->
<!--          placeholder="请输入团队简介"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="团队海报" prop="teamImg">-->
<!--        <el-input-->
<!--          v-model="queryParams.teamImg"-->
<!--          placeholder="请输入团队海报"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="团队价格" prop="teamPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.teamPrice"-->
<!--          placeholder="请输入团队价格"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['behindTeam:YYYbehindTeam:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['behindTeam:YYYbehindTeam:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['behindTeam:YYYbehindTeam:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['behindTeam:YYYbehindTeam:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="YYYbehindTeamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="团队id" align="center" prop="teamId" />
      <el-table-column label="团队名称" align="center" prop="teamName" />
      <el-table-column label="签约数量" align="center" prop="signedNumber" />
      <el-table-column label="团队简介" align="center" prop="teamInfo" />
      <el-table-column label="团队海报" align="center" prop="teamImg" />
      <el-table-column label="团队价格" align="center" prop="teamPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['behindTeam:YYYbehindTeam:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['behindTeam:YYYbehindTeam:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改所有团队对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="YYYbehindTeamRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="团队名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入团队名称" />
        </el-form-item>
        <el-form-item label="签约数量" prop="signedNumber">
          <el-input v-model="form.signedNumber"   placeholder="请输入签约数量" />
        </el-form-item>
        <el-form-item label="团队简介" prop="teamInfo">
          <el-input v-model="form.teamInfo" placeholder="请输入团队简介" />
        </el-form-item>
        <el-form-item label="团队海报" prop="teamImg">
          <el-input v-model="form.teamImg" placeholder="请输入团队海报" />
        </el-form-item>
        <el-form-item label="团队价格" prop="teamPrice">
          <el-input v-model="form.teamPrice" placeholder="请输入团队价格" />
        </el-form-item>
        <el-divider content-position="center">医生信息</el-divider>
<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" icon="Plus" @click="handleAddSysUser">添加</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" icon="Delete" @click="handleDeleteSysUser">删除</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
        <el-table :data="sysUserList" :row-class-name="rowSysUserIndex" @selection-change="handleSysUserSelectionChange" ref="sysUser">
<!--          <el-table-column type="selection" width="50" align="center" />-->
<!--          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
<!--          <el-table-column label="部门ID" prop="deptId" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.deptId" placeholder="请输入部门ID" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="用户账号" prop="userName" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.userName" placeholder="请输入用户账号" />-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="医生姓名"  prop="nickName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.nickName" disabled placeholder="请输入医生姓名" />
            </template>
          </el-table-column>
<!--          <el-table-column label="用户类型" prop="userType" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-select v-model="scope.row.userType" placeholder="请选择用户类型">-->
<!--                <el-option label="请选择字典生成" value="" />-->
<!--              </el-select>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="用户邮箱" prop="email" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.email" placeholder="请输入用户邮箱" />-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="手机号码" prop="phonenumber" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.phonenumber" disabled placeholder="请输入手机号码" />
            </template>
          </el-table-column>
          <el-table-column label="用户性别" prop="sex" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.sex" disabled placeholder="请选择用户性别">
                <el-option label="男" value="0" />
                <el-option label="女" value="1" />
              </el-select>
            </template>
          </el-table-column>
<!--          <el-table-column label="头像地址" prop="avatar" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.avatar" placeholder="请输入头像地址" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="密码" prop="password" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.password" placeholder="请输入密码" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="账号状态" prop="status" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-select v-model="scope.row.status" placeholder="请选择账号状态">-->
<!--                <el-option label="请选择字典生成" value="" />-->
<!--              </el-select>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="最后登录IP" prop="loginIp" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.loginIp" placeholder="请输入最后登录IP" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="最后登录时间" prop="loginDate" width="240">-->
<!--            <template #default="scope">-->
<!--              <el-date-picker clearable-->
<!--                v-model="scope.row.loginDate"-->
<!--                type="date"-->
<!--                value-format="YYYY-MM-DD"-->
<!--                placeholder="请选择最后登录时间">-->
<!--              </el-date-picker>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="密码最后更新时间" prop="pwdUpdateDate" width="240">-->
<!--            <template #default="scope">-->
<!--              <el-date-picker clearable-->
<!--                v-model="scope.row.pwdUpdateDate"-->
<!--                type="date"-->
<!--                value-format="YYYY-MM-DD"-->
<!--                placeholder="请选择密码最后更新时间">-->
<!--              </el-date-picker>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="年龄" prop="userAge" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.userAge" disabled placeholder="请输入年龄" />
            </template>
          </el-table-column>
<!--          <el-table-column label="签约id" prop="contratId" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.contratId" placeholder="请输入签约id" />-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="医生科属" prop="departmentName" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.departmentName" disabled placeholder=" " />
            </template>
          </el-table-column>

<!--          <el-table-column label="医生科属" prop="departmentTypeId" width="150">-->
<!--            <template #default="scope">-->
<!--              <el-input v-model="scope.row.departmentTypeId" placeholder="请输入医生科属" />-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="YYYbehindTeam">
import { listYYYbehindTeam, getYYYbehindTeam, delYYYbehindTeam, addYYYbehindTeam, updateYYYbehindTeam } from "@/api/behindTeam/YYYbehindTeam"

const { proxy } = getCurrentInstance()

const YYYbehindTeamList = ref([])
const sysUserList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedSysUser = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teamName: null,
    signedNumber: null,
    teamInfo: null,
    teamImg: null,
    teamPrice: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询所有团队列表 */
function getList() {
  loading.value = true
  listYYYbehindTeam(queryParams.value).then(response => {
    YYYbehindTeamList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    teamId: null,
    teamName: null,
    signedNumber: null,
    teamInfo: null,
    teamImg: null,
    teamPrice: null
  }
  sysUserList.value = []
  proxy.resetForm("YYYbehindTeamRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.teamId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加团队信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _teamId = row.teamId || ids.value
  getYYYbehindTeam(_teamId).then(response => {
    form.value = response.data
    sysUserList.value = response.data.sysUserList
    open.value = true
    title.value = "修改团队信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["YYYbehindTeamRef"].validate(valid => {
    if (valid) {
      form.value.sysUserList = sysUserList.value
      if (form.value.teamId != null) {
        updateYYYbehindTeam(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addYYYbehindTeam(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _teamIds = row.teamId || ids.value
  proxy.$modal.confirm('是否确认删除所有团队编号为"' + _teamIds + '"的数据项？').then(function() {
    return delYYYbehindTeam(_teamIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 用户信息序号 */
function rowSysUserIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 用户信息添加按钮操作 */
function handleAddSysUser() {
  let obj = {}
  obj.deptId = ""
  obj.userName = ""
  obj.nickName = ""
  obj.userType = ""
  obj.email = ""
  obj.phonenumber = ""
  obj.sex = ""
  obj.avatar = ""
  obj.password = ""
  obj.status = ""
  obj.loginIp = ""
  obj.loginDate = ""
  obj.pwdUpdateDate = ""
  obj.remark = ""
  obj.userAge = ""
  obj.contratId = ""
  obj.departmentTypeId = ""
  obj.departmentName = ""
  sysUserList.value.push(obj)
}

/** 用户信息删除按钮操作 */
function handleDeleteSysUser() {
  if (checkedSysUser.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的用户信息数据")
  } else {
    const sysUsers = sysUserList.value
    const checkedSysUsers = checkedSysUser.value
    sysUserList.value = sysUsers.filter(function(item) {
      return checkedSysUsers.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handleSysUserSelectionChange(selection) {
  checkedSysUser.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('behindTeam/YYYbehindTeam/export', {
    ...queryParams.value
  }, `YYYbehindTeam_${new Date().getTime()}.xlsx`)
}

getList()
</script>
